#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>

struct ThreadNum  //结构体
{
    int thread_num_;   //成员变量 
    //结构体成员变量或类的成员变量命名要注意区分，加个前面或后面加个下划线_
};

void* MyThreadStart(void* arg)
{
    struct ThreadNum* tn = (struct ThreadNum*)arg;//tn是临时变量
    while(1)
    {
        printf("MyThreadStart:%d\n",tn->thread_num_);
        sleep(1);
    }
    delete tn; //在线程退出之前释放开辟的空间
    return NULL;
}
int main()
{
    pthread_t tid;
    for(int i = 0;i < 2; i++)
    {
        struct ThreadNum* tn = new ThreadNum;//结构体指针
        if(tn == NULL) //判断是否开辟成功
        {
            exit(1); //头文件stdlib.h
        }

        tn->thread_num_ = i;
        int ret = pthread_create(&tid,NULL,MyThreadStart,(void*)tn);
        if(ret != 0)
        {
            perror("pthread_create");
            return 0;
        }
        //delete tn; //不能在这释放空间，
                     //因为线程还在访问，在这释放掉造成线程的非法访问
    }

    while(1)
    {
        printf("i am main thread\n");
        sleep(1);
    }
    return 0;
}
